package com.community;

import java.io.File;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.forever.common.FileManager;
import com.forever.common.MyUtil;
import com.forever.common.dao.CommonDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class GallaryAction extends ActionSupport implements Preparable, ModelDriven<GallaryDTO>{

	private static final long serialVersionUID = 1L;
	
	private GallaryDTO dto;
	
	private int FileNum;
	private String pageNum;
	private List<Object> lists;
	private InputStream inputStream;
	private String saveFileName;
	
	public GallaryDTO getDto() {
		return dto;
	}

	public void setDto(GallaryDTO dto) {
		this.dto = dto;
	}

	public int getFileNum() {
		return FileNum;
	}

	public void setFileNum(int fileNum) {
		FileNum = fileNum;
	}

	public String getPageNum() {
		return pageNum;
	}

	public void setPageNum(String pageNum) {
		this.pageNum = pageNum;
	}

	public List<Object> getLists() {
		return lists;
	}

	public void setLists(List<Object> lists) {
		this.lists = lists;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getSaveFileName() {
		return saveFileName;
	}

	public void setSaveFileName(String saveFileName) {
		this.saveFileName = saveFileName;
	}

	public CommonDAO getDao() {
		return dao;
	}

	public void setDao(CommonDAO dao) {
		this.dao = dao;
	}

	public MyUtil getMyUtil() {
		return myUtil;
	}

	public void setMyUtil(MyUtil myUtil) {
		this.myUtil = myUtil;
	}

	@Resource(name="dao")
	private CommonDAO dao;
	
	@Resource(name="myUtil")
	private MyUtil myUtil;

	@Override
	public GallaryDTO getModel() {
		
		return dto;
	}

	@Override
	public void prepare() throws Exception {
		
		dto = new GallaryDTO();
		
	}
	
	public String save() throws Exception{
		
		if(dto == null || dto.getMode() == null || dto.getMode().equals(""))
			return INPUT;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String root = session.getServletContext().getRealPath("/");
		String savePath = root + File.separator + "pds" + File.separator + "image";
		
		String newFileName = FileManager.doFileUpload(dto.getUpload(), dto.getUploadFileName(), savePath);
		String imagePath = "../pds/data/";
		request.setAttribute("imagePath", imagePath);
		if(newFileName != null){
			
			int maxImageNum = dao.getIntValue("gallary.imageMaxNum");
			dto.setNum(maxImageNum + 1);
			dto.setSaveFileName(newFileName);	
			dao.insertData("gallary.insertData", dto);
			System.out.print(dto.getSaveFileName());
			
		}
		
		return SUCCESS;
		
	}
	
	public String gallary() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String cp = request.getContextPath();
		
		int numPerPage = 9;
		int currentPage = 1;
		int totalPage = 0;
		int totalDataCount = 0;
		String imagePath = "../pds/image/";
		request.setAttribute("imagePath", imagePath);
		if(pageNum != null && !(pageNum.equals("")))
			currentPage = Integer.parseInt(pageNum);
		
		String searchKey = request.getParameter("searchKey");
		String searchValue = request.getParameter("searchValue");
		
		if(searchKey == null){
			
			searchKey = "subject";
			searchValue = "";
		}
		
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("searchKey", searchKey);
		hMap.put("searchValue", searchValue);
		int start = (currentPage - 1) * numPerPage + 1;
		int end = currentPage * numPerPage;
		
		hMap.put("start", start);
		hMap.put("end", end);
		hMap.put("area", 1);
		
		totalDataCount = dao.getIntValue("gallary.dataCount",1);
		
		
		if(totalDataCount != 0)
			totalPage = myUtil.getPageCount(numPerPage, totalDataCount);
		
		if(currentPage > totalPage)
			currentPage = totalPage;
		
		lists = (List<Object>)dao.getListData("gallary.listData_s", hMap);
		
		int listNum, n = 0;
		String str;
		ListIterator<Object> it = lists.listIterator();
		
		while(it.hasNext()){
			
			GallaryDTO dto = (GallaryDTO)it.next();
			listNum = totalDataCount - (start + n - 1);
			dto.setListNum(listNum);
			n++;
			
			str = cp + "/community/download.action?fileNum=" + dto.getNum();
			dto.setUrlFile(str);
			
		}
		
		String params = "";
		
		if(!searchValue.equals("")){
			searchValue = URLEncoder.encode(searchValue, "UTF-8");
			params = "searchKey=" +searchKey;
			params+= "&searchValue=" +searchValue;
		}
			
		String urlList = cp + "/community/gallary.action";
		request.setAttribute("pageNum", currentPage);
		request.setAttribute("totalDataCount", Integer.toString(totalDataCount));
		request.setAttribute("totalPage", Integer.toString(totalPage));
		request.setAttribute("pageIndexList", myUtil.pageIndexList(currentPage, totalPage, urlList));	
		
		return SUCCESS;
			
	}
	
	public String gallery2() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String cp = request.getContextPath();
		
		int numPerPage = 9;
		int currentPage = 1;
		int totalPage = 0;
		int totalDataCount = 0;
		String imagePath = "../pds/image/";
		request.setAttribute("imagePath", imagePath);
		if(pageNum != null && !(pageNum.equals("")))
			currentPage = Integer.parseInt(pageNum);
		
		String searchKey = request.getParameter("searchKey");
		String searchValue = request.getParameter("searchValue");
		
		if(searchKey == null){
			
			searchKey = "subject";
			searchValue = "";
		}
		
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("searchKey", searchKey);
		hMap.put("searchValue", searchValue);
		int start = (currentPage - 1) * numPerPage + 1;
		int end = currentPage * numPerPage;
		
		hMap.put("start", start);
		hMap.put("end", end);
		hMap.put("area", 2);
		
		totalDataCount = dao.getIntValue("gallary.dataCount",2);
		
		
		if(totalDataCount != 0)
			totalPage = myUtil.getPageCount(numPerPage, totalDataCount);
		
		if(currentPage > totalPage)
			currentPage = totalPage;
		
		lists = (List<Object>)dao.getListData("gallary.listData_s", hMap);
		
		int listNum, n = 0;
		String str;
		ListIterator<Object> it = lists.listIterator();
		
		while(it.hasNext()){
			
			GallaryDTO dto = (GallaryDTO)it.next();
			listNum = totalDataCount - (start + n - 1);
			dto.setListNum(listNum);
			n++;
			
			str = cp + "/community/download.action?fileNum=" + dto.getNum();
			dto.setUrlFile(str);
			
		}
		
		String params = "";
		
		if(!searchValue.equals("")){
			searchValue = URLEncoder.encode(searchValue, "UTF-8");
			params = "searchKey=" +searchKey;
			params+= "&searchValue=" +searchValue;
		}
			
		String urlList = cp + "/community/gallery2.action";
		request.setAttribute("pageNum", currentPage);
		request.setAttribute("totalDataCount", Integer.toString(totalDataCount));
		request.setAttribute("totalPage", Integer.toString(totalPage));
		request.setAttribute("pageIndexList", myUtil.pageIndexList(currentPage, totalPage, urlList));	
		
		return SUCCESS;
			
	}
	
	public String gallery3() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String cp = request.getContextPath();
		
		int numPerPage = 9;
		int currentPage = 1;
		int totalPage = 0;
		int totalDataCount = 0;
		String imagePath = "../pds/image/";
		request.setAttribute("imagePath", imagePath);
		if(pageNum != null && !(pageNum.equals("")))
			currentPage = Integer.parseInt(pageNum);
		
		String searchKey = request.getParameter("searchKey");
		String searchValue = request.getParameter("searchValue");
		
		if(searchKey == null){
			
			searchKey = "subject";
			searchValue = "";
		}
		
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("searchKey", searchKey);
		hMap.put("searchValue", searchValue);
		int start = (currentPage - 1) * numPerPage + 1;
		int end = currentPage * numPerPage;
		
		hMap.put("start", start);
		hMap.put("end", end);
		hMap.put("area", 3);
		
		totalDataCount = dao.getIntValue("gallary.dataCount",3);
		
		
		if(totalDataCount != 0)
			totalPage = myUtil.getPageCount(numPerPage, totalDataCount);
		
		if(currentPage > totalPage)
			currentPage = totalPage;
		
		lists = (List<Object>)dao.getListData("gallary.listData_s", hMap);
		
		int listNum, n = 0;
		String str;
		ListIterator<Object> it = lists.listIterator();
		
		while(it.hasNext()){
			
			GallaryDTO dto = (GallaryDTO)it.next();
			listNum = totalDataCount - (start + n - 1);
			dto.setListNum(listNum);
			n++;
			
			str = cp + "/community/download.action?fileNum=" + dto.getNum();
			dto.setUrlFile(str);
			
		}
		
		String params = "";
		
		if(!searchValue.equals("")){
			searchValue = URLEncoder.encode(searchValue, "UTF-8");
			params = "searchKey=" +searchKey;
			params+= "&searchValue=" +searchValue;
		}
			
		String urlList = cp + "/community/gallery3.action";
		request.setAttribute("pageNum", currentPage);
		request.setAttribute("totalDataCount", Integer.toString(totalDataCount));
		request.setAttribute("totalPage", Integer.toString(totalPage));
		request.setAttribute("pageIndexList", myUtil.pageIndexList(currentPage, totalPage, urlList));	
		
		return SUCCESS;
			
	}
	
	public String gallery4() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String cp = request.getContextPath();
		
		int numPerPage = 9;
		int currentPage = 1;
		int totalPage = 0;
		int totalDataCount = 0;
		String imagePath = "../pds/image/";
		request.setAttribute("imagePath", imagePath);
		if(pageNum != null && !(pageNum.equals("")))
			currentPage = Integer.parseInt(pageNum);
		
		String searchKey = request.getParameter("searchKey");
		String searchValue = request.getParameter("searchValue");
		
		if(searchKey == null){
			
			searchKey = "subject";
			searchValue = "";
		}
		
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("searchKey", searchKey);
		hMap.put("searchValue", searchValue);
		int start = (currentPage - 1) * numPerPage + 1;
		int end = currentPage * numPerPage;
		
		hMap.put("start", start);
		hMap.put("end", end);
		hMap.put("area", 4);
		
		totalDataCount = dao.getIntValue("gallary.dataCount",4);
		
		
		if(totalDataCount != 0)
			totalPage = myUtil.getPageCount(numPerPage, totalDataCount);
		
		if(currentPage > totalPage)
			currentPage = totalPage;
		
		lists = (List<Object>)dao.getListData("gallary.listData_s", hMap);
		
		int listNum, n = 0;
		String str;
		ListIterator<Object> it = lists.listIterator();
		
		while(it.hasNext()){
			
			GallaryDTO dto = (GallaryDTO)it.next();
			listNum = totalDataCount - (start + n - 1);
			dto.setListNum(listNum);
			n++;
			
			str = cp + "/community/download.action?fileNum=" + dto.getNum();
			dto.setUrlFile(str);
			
		}
		
		String params = "";
		
		if(!searchValue.equals("")){
			searchValue = URLEncoder.encode(searchValue, "UTF-8");
			params = "searchKey=" +searchKey;
			params+= "&searchValue=" +searchValue;
		}
			
		String urlList = cp + "/community/gallery4.action";
		request.setAttribute("pageNum", currentPage);
		request.setAttribute("totalDataCount", Integer.toString(totalDataCount));
		request.setAttribute("totalPage", Integer.toString(totalPage));
		request.setAttribute("pageIndexList", myUtil.pageIndexList(currentPage, totalPage, urlList));	
		
		return SUCCESS;
			
	}
	
	public String gallery5() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String cp = request.getContextPath();
		
		int numPerPage = 9;
		int currentPage = 1;
		int totalPage = 0;
		int totalDataCount = 0;
		String imagePath = "../pds/image/";
		request.setAttribute("imagePath", imagePath);
		if(pageNum != null && !(pageNum.equals("")))
			currentPage = Integer.parseInt(pageNum);
		
		String searchKey = request.getParameter("searchKey");
		String searchValue = request.getParameter("searchValue");
		
		if(searchKey == null){
			
			searchKey = "subject";
			searchValue = "";
		}
		
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("searchKey", searchKey);
		hMap.put("searchValue", searchValue);
		int start = (currentPage - 1) * numPerPage + 1;
		int end = currentPage * numPerPage;
		
		hMap.put("start", start);
		hMap.put("end", end);
		hMap.put("area", 5);
		
		totalDataCount = dao.getIntValue("gallary.dataCount",5);
		
		
		if(totalDataCount != 0)
			totalPage = myUtil.getPageCount(numPerPage, totalDataCount);
		
		if(currentPage > totalPage)
			currentPage = totalPage;
		
		lists = (List<Object>)dao.getListData("gallary.listData_s", hMap);
		
		int listNum, n = 0;
		String str;
		ListIterator<Object> it = lists.listIterator();
		
		while(it.hasNext()){
			
			GallaryDTO dto = (GallaryDTO)it.next();
			listNum = totalDataCount - (start + n - 1);
			dto.setListNum(listNum);
			n++;
			
			str = cp + "/community/download.action?fileNum=" + dto.getNum();
			dto.setUrlFile(str);
			
		}
		
		String params = "";
		
		if(!searchValue.equals("")){
			searchValue = URLEncoder.encode(searchValue, "UTF-8");
			params = "searchKey=" +searchKey;
			params+= "&searchValue=" +searchValue;
		}
			
		String urlList = cp + "/community/gallery5.action";
		request.setAttribute("pageNum", currentPage);
		request.setAttribute("totalDataCount", Integer.toString(totalDataCount));
		request.setAttribute("totalPage", Integer.toString(totalPage));
		request.setAttribute("pageIndexList", myUtil.pageIndexList(currentPage, totalPage, urlList));	
		
		return SUCCESS;
			
	}
	
	public String delete() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String root = session.getServletContext().getRealPath("/");
		String savePath = root + File.separator + "pds" + File.separator + "image";
	
		int fileNum = Integer.parseInt(request.getParameter("fileNum"));
		GallaryDTO file = (GallaryDTO)dao.getReadData("gallary.imageName", fileNum);
		FileManager.doFileUpload(null, file.getSaveFileName(), savePath);
		
		dao.deleteData("gallary.deleteData", fileNum);
		
		return SUCCESS;
		
	}
	
	public String delete2() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String root = session.getServletContext().getRealPath("/");
		String savePath = root + File.separator + "pds" + File.separator + "image";
	
		int fileNum = Integer.parseInt(request.getParameter("fileNum"));
		GallaryDTO file = (GallaryDTO)dao.getReadData("gallary.imageName", fileNum);
		FileManager.doFileUpload(null, file.getSaveFileName(), savePath);
		
		dao.deleteData("gallary.deleteData", fileNum);
		
		return SUCCESS;
		
	}
	
	public String delete3() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String root = session.getServletContext().getRealPath("/");
		String savePath = root + File.separator + "pds" + File.separator + "image";
	
		int fileNum = Integer.parseInt(request.getParameter("fileNum"));
		GallaryDTO file = (GallaryDTO)dao.getReadData("gallary.imageName", fileNum);
		FileManager.doFileUpload(null, file.getSaveFileName(), savePath);
		
		dao.deleteData("gallary.deleteData", fileNum);
		
		return SUCCESS;
		
	}
	
	public String delete4() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String root = session.getServletContext().getRealPath("/");
		String savePath = root + File.separator + "pds" + File.separator + "image";
	
		int fileNum = Integer.parseInt(request.getParameter("fileNum"));
		GallaryDTO file = (GallaryDTO)dao.getReadData("gallary.imageName", fileNum);
		FileManager.doFileUpload(null, file.getSaveFileName(), savePath);
		
		dao.deleteData("gallary.deleteData", fileNum);
		
		return SUCCESS;
		
	}
	
	public String delete5() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String root = session.getServletContext().getRealPath("/");
		String savePath = root + File.separator + "pds" + File.separator + "image";
	
		int fileNum = Integer.parseInt(request.getParameter("fileNum"));
		GallaryDTO file = (GallaryDTO)dao.getReadData("gallary.imageName", fileNum);
		FileManager.doFileUpload(null, file.getSaveFileName(), savePath);
		
		dao.deleteData("gallary.deleteData", fileNum);
		
		return SUCCESS;
		
	}
}
